package com.pzhu.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pzhu.entity.Business;
import com.pzhu.entity.Code;
import com.pzhu.entity.Hotel;
import com.pzhu.entity.Result;
import com.pzhu.service.HotelService;
import com.pzhu.until.JwtTokenUntil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@RestController
@RequestMapping("api/hotel")
public class HotelController {
    @Autowired
    private HotelService hotelService;

    /**
     * 分页条件查询酒店
     * @param request
     * @param current
     * @param size
     * @param hotelName
     * @param hotelAddress
     * @return
     */
    @GetMapping
    public Result select(
            HttpServletRequest request,
            @RequestParam("current") Integer current,
            @RequestParam("size") Integer size,
            @RequestParam("hotelName") String hotelName,
            @RequestParam("hotelAddress") String hotelAddress) {


        Integer loginBusiness = JwtTokenUntil.getClaim(request, "loginBusiness").asInt();
        LambdaQueryWrapper<Hotel> hotelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        Page<Hotel> orderPage = new Page<>(current, size);

        hotelLambdaQueryWrapper.eq(Hotel::getHotelBusinessId, loginBusiness);

        hotelLambdaQueryWrapper.like(hotelName != null, Hotel::getHotelName, hotelName);
        hotelLambdaQueryWrapper.like(hotelAddress != null, Hotel::getHotelAddress, hotelAddress);


        Page<Hotel> page = hotelService.page(orderPage, hotelLambdaQueryWrapper);


        return new Result(Code.success, "", page);

    }

    /**
     * 用户分页查询酒店
     * @param current
     * @param size
     * @return
     */
    @GetMapping("u")
    public Result select(
            @RequestParam("current") Integer current,
            @RequestParam("size") Integer size

    ) {
        LambdaQueryWrapper<Hotel> hotelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        Page<Hotel> orderPage = new Page<>(current, size);

        Page<Hotel> page = hotelService.page(orderPage, hotelLambdaQueryWrapper);


        return new Result(Code.success, "", page);

    }

    /**
     * 添加酒店
     * @param request
     * @param hotel
     * @param multipartFile
     * @return
     * @throws IOException
     */
    @PostMapping
    public Result add(HttpServletRequest request, @RequestParam("hotel") String hotel, @RequestParam MultipartFile multipartFile) throws IOException {


        Hotel hotel1 = JSON.parseObject(hotel, Hotel.class);
        byte[] bytes = multipartFile.getInputStream().readAllBytes();


        Integer loginBusiness = JwtTokenUntil.getClaim(request, "loginBusiness").asInt();

        hotel1.setHotelBusinessId(loginBusiness);
        hotel1.setHotelImg(bytes);
        boolean save = hotelService.save(hotel1);
        return new Result(Code.success, "", save);
    }

    /**
     * 删除酒店
     * @param hotel
     * @return
     */
    @DeleteMapping
    public Result delete(@RequestBody Hotel hotel) {

        boolean b = hotelService.removeById(hotel);
        return new Result(Code.success, "", b);

    }

    /**
     * 修改酒店包括图片
     * @param hotel
     * @param multipartFile
     * @return
     * @throws IOException
     */
    @PutMapping()
    private Result update(@RequestParam("hotel") String hotel, @RequestParam MultipartFile multipartFile) throws IOException {

        Hotel hotel1 = JSON.parseObject(hotel, Hotel.class);
        byte[] bytes = multipartFile.getInputStream().readAllBytes();
        hotel1.setHotelImg(bytes);
        boolean b = hotelService.updateById(hotel1);
        return new Result(Code.success, "", b);

    }

    /**
     * 修改酒店不包括图片
     * @param hotel
     * @return
     */
    @PutMapping("/edit")
    private Result update(@RequestParam("hotel") String hotel) {
        Hotel hotel1 = JSON.parseObject(hotel, Hotel.class);
        boolean b = hotelService.updateById(hotel1);
        return new Result(Code.success, "", b);
    }
}
